﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utilities;

namespace EasyExcel
{
    /// <summary>
    /// Excel文档对象
    /// </summary>
    public class ExcelDoc
    {
        private const string mbFileName = "Cfg/Mb.xlsx";
        private string saveExcelFile;
        public NpoiExcel Npoi{ get; } = new NpoiExcel(mbFileName);        
        public ExcelDoc()
        {
            
        }
        /// <summary>
        /// 已经导出成功的Sheet名称
        /// </summary>
        public List<string> hadExp = new List<string>();
        /// <summary>
        /// 打印预览
        /// </summary>
        public bool PrintPreView()
        {
            SaveTmp();
            //System.Threading.Thread.Sleep(500);
            if (string.IsNullOrEmpty(saveExcelFile)) return false;
            ExportToExcel exp = new ExportToExcel(saveExcelFile);
            exp.PrintPreView();
            exp.CloseAll();
            try
            {
                if (File.Exists(saveExcelFile)) File.Delete(saveExcelFile);
            }
            catch { }

            return true;
        }
        /// <summary>
        /// 设置当前Sheet
        /// </summary>
        /// <param name="sn"></param>
        /// <returns></returns>
        public bool SetSheetByName(string sn)
        {
            Npoi.errorMsg = string.Empty;
            Npoi.setSheetByName(sn);
            return string.IsNullOrEmpty(Npoi.errorMsg);
        }
        /// <summary>
        /// 设置单元格的值
        /// </summary>
        /// <param name="r">行：序号0开始</param>
        /// <param name="c">列：序号0开始</param>
        /// <param name="obj"></param>
        public void SetCell(int r, int c, object obj)
        {
            Npoi.setCellValue(r, c, obj);
        }
        /// <summary>
        /// 复制当前行，插入下一行
        /// </summary>
        /// <param name="currentRow"></param>
        public void InsertRow(int currentRow)
        {
            Npoi.InsertCopyRow(currentRow);
        }

        /// <summary>
        /// 公式重新刷新
        /// </summary>
        public void Recalculation()
        {
            Npoi.RecountFormula();
        }
        /// <summary>
        /// 保存临时文件
        /// </summary>
        /// <returns></returns>
        private bool SaveTmp()
        {
            DelSheetList();
            Recalculation();
            string tmpFileName = DateTime.Now.Ticks + "tmp.xlsx";
            Npoi.SaveAs(tmpFileName);
            saveExcelFile = tmpFileName;
            return true;
        }
        /// <summary>
        /// 导出到Pdf文件
        /// </summary>
        /// <param name="pdfName"></param>
        /// <returns></returns>
        public bool ExpPdf(string pdfName)
        {
            if (string.IsNullOrEmpty(saveExcelFile)) return false;
            ExportToExcel exp = new ExportToExcel(saveExcelFile);
            exp.Convert2PDF(saveExcelFile);
            return true;
        }
        /// <summary>
        /// 删除不在清单中的工作表
        /// </summary>
        /// <param name="sourceName"></param>
        private void DelSheetList()
        {
            Npoi.DelSheetList(hadExp);
        }
    }
}
